<template>
	<view>
		<navbar title="应用列表">
		</navbar>
		<view style="display: flex;align-items: center;padding: 5px;">
			<u-search :showAction="false" style="background-color: white;padding: 0 10px;" placeholder="请输入应用名称或包名"
				@blur="search"></u-search>
		</view>
		<view style="height: 30px;display: flex;font-size: 14px;align-items: center;">
			<p style="margin: 0 10px;">类型:</p>
			<u-radio-group v-model="radio2" placement="row">
				<u-radio :customStyle="{marginRight: '5px'}" v-for="(item, index) in apptype" :key="index"
					:label="item.text" :name="item.value">
				</u-radio>
			</u-radio-group>
		</view>
		<view style="height: 30px;display: flex;font-size: 14px;align-items: center;">
			<p style="margin: 0 10px;">排序:</p>
			<u-radio-group v-model="radio1" placement="row">
				<u-radio :customStyle="{marginRight: '5px'}" v-for="(item, index) in sort" :key="index"
					:label="item.text" :name="item.value">
				</u-radio>
			</u-radio-group>
		</view>
		<view v-for="(item,index) in list" @click="upload(item.name,index)" style="position: relative;">
			<view class="progress" :style="{'width': item.progress}">
				<uni-icons v-if="item.progress=='100%'" color="#42b983" type="checkmarkempty"
					style="margin-right: 10px;" size="50"></uni-icons>
			</view>
			<view style="position: relative;width: 100%;display: flex;align-items: center;padding: 5px;">
				<image :src="item.icon" style="width: 50px;height: 50px;border-radius: 10px;"></image>
				<view style="margin-left: 5px;font-size: 12px;">
					<p style="font-size: 16px;">{{item.name}}</p>
					<p>{{gsbytes(item.size)}} | V{{item.version}}</p>
					<p>{{item.package}}</p>
				</view>
			</view>
		</view>
		<!-- 上传进度 -->
		<u-popup :round="10" :show="show">
			<view style="padding: 10px;border-radius: 10px 10px 0 0;background-color: white;">
				<h3>上传进度</h3>
				<view v-if="show" style="display: flex;align-items: center;margin: 10px 0;">
					<image :src="temp.icon" style="width: 50px;height: 50px;"></image>
					<view style="margin-left: 10px;font-size: 12px;">
						<p style="font-size: 16px;">{{temp.name}}</p>
						<p>{{gsbytes(temp.size)}} | V{{temp.version}}</p>
						<p>{{temp.package}}</p>
					</view>
				</view>
				<u-line-progress activeColor="#85bcff" style="margin: 10px 0;" :percentage="progress"></u-line-progress>
			</view>
		</u-popup>
		<!-- 上传进度条 -->
		<UploadProgress ref="UploadProgress" :loading="progressLoading" :show="progressShow" :list="[]"
			@close="progressShow=false">
		</UploadProgress>
	</view>
</template>

<script>
	import store from '@/store/index.js';
	const db = uniCloud.database();
	import {
		pathToBase64,
		base64ToPath
	} from 'image-tools'
	const getapplist = uni.requireNativePlugin('Fengci-AppList')
	export default {
		data() {
			return {
				progressLoading: false,
				progressShow: false,
				success: [],
				progress: 0,
				temp: null,
				show: false,
				radio1: null,
				radio2: 0,
				apptype: [{
					text: '用户应用',
					value: 0
				}, {
					text: '全部应用',
					value: 1
				}],
				sort: [{
					text: '软件大小',
					value: 0
				}, {
					text: '安装时间',
					value: 1
				}, {
					text: '更新时间',
					value: 2
				}],
				list: [],
				up: null,
				maxupsize: 1024 * 1024 * 50,
				userInfo: store.state.userInfo,
				waitAdd: []
			};
		},
		watch: {
			radio2(val) {
				if (val == 0) {
					this.getlist()
				} else {
					this.getalllist()
				}
			},
			radio1(val) {
				if (val == 0) {
					this.list = this.sortarr(this.list, 'size')
				} else if (val == 1) {
					this.list = this.sortarr(this.list, 'firstDate')
				} else if (val == 2) {
					this.list = this.sortarr(this.list, 'lastDate')
				}
			}
		},
		onUnload() {
			uni.$emit("addFile", this.waitAdd)
		},
		methods: {
			sortarr(arr, key) {
				for (var i = 0; i < arr.length; i++) {
					for (var n = i; n < arr.length; n++) {
						if (arr[n][key] > arr[i][key]) {
							var temp = arr[n];
							arr[n] = arr[i];
							arr[i] = temp;
						}
					}
				}
				return arr
			},
			search(val) {
				if (this.list.length == 0) {
					this.message('应用列表为空', 'error')
					return null
				}
				var temp = []
				for (var i in this.list) {
					if (this.list[i].name.indexOf(val) >= 0 || this.list[i].package.indexOf(val) >= 0) {
						temp.push(this.list[i])
					}
				}
				this.list = temp
			},
			async getalllist() {
				uni.showLoading({
					title: '全部应用加载中'
				});
				this.list = await getapplist.allapp()
				uni.hideLoading()
			},
			async getlist() {
				uni.showLoading({
					title: '本地应用加载中'
				});
				this.list = await getapplist.userapp()
				uni.hideLoading()
			},
			upload(name, index) {
				var that = this
				if (that.success.indexOf(index) >= 0) {
					this.message('应用已经上传', 'error')
					return null
				}
				uni.showModal({
					title: '提示',
					content: '确认上传应用' + name + '?',
					success: function(res) {
						if (res.confirm) {
							that.uploadapk(index)
						}
					}
				});
			},
			uploadapk(index) {
				if (this.list[index].size >= 1024 * 1024 * 100) {
					this.message("文件不得超过100MB", "error")
					return
				}
				let that = this
				this.progressLoading = true
				this.progressShow = true
				let form = {
					user_id: this.userInfo._id,
					version: this.list[index].version,
					size: this.list[index].size,
					package: this.list[index].package,
					title: this.list[index].name,
					type: "apk",
					weight: 2,
					up: this.up,
					time: new Date(),
					openup: "apk_moren"
				}
				base64ToPath(this.list[index].icon)
					.then(async function(iconPath) {
						that.uploadFile(iconPath, function(upRes) {
							form.icon = upRes
							that.uploadFileToLanzou(that.list[index].path.replace("file://", ""), (
								upLanRes) => {
								let downInfo = JSON.parse(upLanRes.data)
								let pwdInfo = JSON.parse(upLanRes.password)
								let lanzouUrl =
									`${downInfo.text[0].is_newd}/${downInfo.text[0].f_id}?pwd=${pwdInfo.info.pwd}`
								form.url = lanzouUrl
								db.collection("item").add(form).then(dbRes => {
									that.progressLoading = false
									that.progressShow = false
									that.waitAdd.push(dbRes.result.id)
									that.success.push(index)
									that.message("上传成功")
								})
							})


						})
					})
			}
		},
		onLoad(op) {
			this.up = op.up
			this.getlist()
		}
	}
</script>
<style lang="less">
	page {
		background-color: white;
	}

	.progress {
		position: absolute;
		background-color: #85bcff;
		bottom: 0;
		top: 0;
		display: flex;
		align-items: center;
		justify-content: flex-end;
	}
</style>